package com.bedrock.project.inside.api.v1;

import com.bedrock.common.utils.poi.ExcelUtil;
import com.bedrock.framework.aspectj.lang.annotation.Log;
import com.bedrock.framework.aspectj.lang.enums.BusinessType;
import com.bedrock.framework.web.controller.BaseController;
import com.bedrock.framework.web.domain.AjaxResult;
import com.bedrock.project.inside.entity.WorkHourType;
import com.bedrock.project.inside.service.IWorkHourTypeService;
import com.bedrock.project.inside.vo.response.WorkHourTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工时分类Controller
 * 
 * @author bedrock
 * @date 2020-04-22
 */
@RestController
@RequestMapping("/work_hour/type")
public class WorkHourTypeController extends BaseController
{
    @Autowired
    private IWorkHourTypeService workHourTypeService;

    /**
     * 查询工时分类列表
     */
    /*@PreAuthorize("@ss.hasPermi('work_hour:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkHourType workHourType)
    {
        startPage();
        List<WorkHourType> list = workHourTypeService.selectWorkHourTypeList(workHourType);
        return getDataTable(list);
    }*/

    /**
     * 获取工时分类列表
     */
    //@PreAuthorize("@ss.hasPermi('pmp:pipline:list')")
    @GetMapping("/list")
    public AjaxResult list()
    {
        List<WorkHourTypeVo> list = new ArrayList<WorkHourTypeVo>();
        Map<Long, List<WorkHourType>> data = workHourTypeService.selectWorkHourTypeList(null).stream().collect(Collectors.groupingBy(WorkHourType::getParentId));
        for(WorkHourType workHourType : data.get(0L)){
            WorkHourTypeVo temp = new WorkHourTypeVo();
            temp.setLabel(workHourType.getWorkHourTypeName());
            temp.setValue(workHourType.getWorkHourTypeId());
            List<WorkHourTypeVo> children = new ArrayList<WorkHourTypeVo>();
            for(WorkHourType child : data.get(workHourType.getWorkHourTypeId())){
                WorkHourTypeVo temp2 = new WorkHourTypeVo();
                temp2.setLabel(child.getWorkHourTypeName());
                temp2.setValue(child.getWorkHourTypeId());
                children.add(temp2);
            }
            temp.setChildren(children);
            list.add(temp);
        }

        /*List<WorkHourType> datas = workHourTypeService.selectWorkHourTypeList(null);

           for (WorkHourType workHourType : datas){
               if (workHourType.getParentId()==0){
                   WorkHourTypeVo temp = new WorkHourTypeVo();
                   temp.setLabel(workHourType.getWorkHourTypeName());
                   temp.setValue(workHourType.getWorkHourTypeId());
                   temp.setChildren(new ArrayList<>());
                   list.add(temp);
               }else{
                   for (WorkHourTypeVo vo : list){
                       if(vo.getValue().equals(workHourType.getParentId())){
                           WorkHourTypeVo temp2 = new WorkHourTypeVo();
                           temp2.setLabel(workHourType.getWorkHourTypeName());
                           temp2.setValue(workHourType.getWorkHourTypeId());
                           vo.getChildren().add(temp2);
                           break;
                       }
                   }
               }
           }*/
        return AjaxResult.success(list);
    }

    /**
     * 导出工时分类列表
     */
    @PreAuthorize("@ss.hasPermi('work_hour:type:export')")
    @Log(title = "工时分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WorkHourType workHourType)
    {
        List<WorkHourType> list = workHourTypeService.selectWorkHourTypeList(workHourType);
        ExcelUtil<WorkHourType> util = new ExcelUtil<WorkHourType>(WorkHourType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取工时分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('work_hour:type:query')")
    @GetMapping(value = "/{workHourTypeId}")
    public AjaxResult getInfo(@PathVariable("workHourTypeId") Long workHourTypeId)
    {
        return AjaxResult.success(workHourTypeService.selectWorkHourTypeById(workHourTypeId));
    }

    /**
     * 新增工时分类
     */
    @PreAuthorize("@ss.hasPermi('work_hour:type:add')")
    @Log(title = "工时分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkHourType workHourType)
    {
        return toAjax(workHourTypeService.insertWorkHourType(workHourType));
    }

    /**
     * 修改工时分类
     */
    @PreAuthorize("@ss.hasPermi('work_hour:type:edit')")
    @Log(title = "工时分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkHourType workHourType)
    {
        return toAjax(workHourTypeService.updateWorkHourType(workHourType));
    }

    /**
     * 删除工时分类
     */
    @PreAuthorize("@ss.hasPermi('work_hour:type:remove')")
    @Log(title = "工时分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workHourTypeIds}")
    public AjaxResult remove(@PathVariable Long[] workHourTypeIds)
    {
        return toAjax(workHourTypeService.deleteWorkHourTypeByIds(workHourTypeIds));
    }
}
